package com.example.springmvc.controller;

import com.example.springmvc.model.UserInfo;
import jakarta.servlet.http.Cookie;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class UserController {
        @RequestMapping("/hello")
        public String hello() {
            return "hello spring mvc";
        }

        @RequestMapping("/param1")
        public String param1(String userName) {
            return "userName:" + userName;
        }

        @RequestMapping("/param2")
        public String param2(String userName,int age) {
            return "userName:" + userName + " age:" + age;
        }

        @RequestMapping("/param3")
        public String param3(String userName,Integer age) {
            return "userName:" + userName + " age:" + age;
        }

        @RequestMapping("/param4")
        public String param4(UserInfo userInfo) {
            return " userInfo:" +  userInfo;
        }

        @RequestMapping("/param5")
        public String param5(String[] arrParams) {
            return " arrParams:" + Arrays.toString(arrParams);
        }

        @RequestMapping("/param6")
        public String param6(@RequestParam(value = "listParams",required = false)List<String> listParams) {
            return " listParams:" +listParams;
        }

        @RequestMapping("/param7")
        public String param7(@RequestBody UserInfo userInfo) {
            return " userInfo:" +userInfo;
    }

    @RequestMapping("/param8/{articleId}")
    public String param8(@PathVariable Integer articleId) {
        return "收到参数 articleId:" + articleId;
    }

    @RequestMapping("/param9/{articleId}/{name}")   //这些参数不能带“/”
    public String param9(@PathVariable Integer articleId,@PathVariable String name) {
        return "收到参数 articleId:" + articleId + " name :" + name;
    }

    @RequestMapping("/param10")
    public String param10(@RequestPart("file1") MultipartFile file) throws IOException {
//        System.out.println(file.getName());
//        System.out.println(file.getOriginalFilename());
//        System.out.println(file.getContentType());
        file.transferTo(new File("E:/test/"+file.getOriginalFilename()));
        return "收到文件:" +file.getOriginalFilename();
    }

    @RequestMapping("/getCookie")//可以获取所有的cookie信息
    public String getCookie(HttpServletRequest request) {
//            String userName = request.getParameter("useName"); //原生servlet做法  从request中拿到参数
//            String password = request.getParameter("password");
              Cookie[] cookies = request.getCookies();
        System.out.println("获取cookie信息");

        if(cookies != null) {
            for(Cookie cookie: cookies) {
                if("bite".equals(cookie.getName())) {

                }
                if(cookie.getName().equals("bite")) {

                }
                System.out.println(cookie.getName()+":"+cookie.getValue());
            }
        }

              return "获取Cookie成功";
    }

    @RequestMapping("/getCookie2")//只能获取其中的一个
    public String getCookie2(@CookieValue("bite") String bite1) {
                System.out.println("从Cookie中获取bite:" + bite1);
        return "获取Cookie成功";
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
       HttpSession session = request.getSession(true);//为true时，不存在session就会创建一个session;为false返回null
       session.setAttribute("name","zhangsan");
       session.setAttribute("studentId","10086");
        return "设置Session成功";
    }
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession(false);//为false，不存在session返回null
        if(session != null) {
            System.out.println(session.getAttribute("name"));
            System.out.println(session.getAttribute("studentId"));
        }
        return "获取Session成功";
    }//重启服务，服务器谁也不认识了，（SessionId 对应的session没有了， 就获取不了cookie信息了，要重新开启会话（创建session,才可以获取session    session默认存在在内存中，一关闭服务数据就会消失
    //要先设置session（重新登录）,再重新获取


    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {
        System.out.println("name: " + session.getAttribute("name"));
        System.out.println("studentId: "+ session.getAttribute("studentId"));
        return "获取Session成功";
    }

    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute (value = "name",required = false)String name) {
        System.out.println("name: " + name);
        return "获取Session成功";
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
            String userAgent = request.getHeader("User-Agent");
            return "userAgent: " + userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader(@RequestHeader("User-Agent")String userAgent) {
        return "userAgent: " + userAgent;
    }
}
